﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using Microsoft.Win32;

namespace MyLibrary.IO
{
    /// <summary>
    /// 调用WinRAR进行.rar 文件操作
    /// </summary>
    public class WinRARCaller
    {
        private static WinRARCaller _instance = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="winRaRPath"></param>
        private WinRARCaller()
        {

        }

        public static WinRARCaller Instance
        {
            get 
            {
                if (_instance == null)
                    _instance = new WinRARCaller();
                return _instance;
            }
        }

        /// <summary>
        /// 解压缩Rar文件
        /// </summary>
        /// <param name="unRarPath">解压缩的路径</param>
        /// <param name="rarfile">Rar文件路径</param>
        /// <returns>操作结果</returns>
        public bool UnRaR(string unRarPath, string rarfile)
        {
            //D:\WinRAR\WinRAR.exe x d:\Temp\wintest.rar d:\Temp -o+ -y

            //从注册表中获取WinRAR的路径
            RegistryKey winRarPathKey = null;
            string winRarPath = String.Empty;

            winRarPathKey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR\Shell\Open\Command");
            if (winRarPathKey == null)
                winRarPathKey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");
            winRarPath = winRarPathKey.GetValue("").ToString();
            winRarPathKey.Close();
            winRarPath = winRarPath.Split(new char[] { '"' }, StringSplitOptions.RemoveEmptyEntries)[0];

            return UnRaR(unRarPath, rarfile, winRarPath);
        }

        /// <summary>
        /// 解压缩Rar文件
        /// </summary>
        /// <param name="unRarPath">解压缩的路径</param>
        /// <param name="rarfile">Rar文件路径</param>
        /// <param name="rarExecute">Rar可执行文件的路径</param>
        /// <returns>操作结果</returns>
        public bool UnRaR(string unRarPath, string rarfile, string rarExecute)
        {
            //D:\WinRAR\WinRAR.exe x d:\Temp\wintest.rar d:\Temp -o+ -y

            if (String.IsNullOrEmpty(unRarPath)) throw new ArgumentNullException("解压路径不能为空");
            if (String.IsNullOrEmpty(rarfile)) throw new ArgumentNullException("压缩文件路径不能为空");

            if (!File.Exists(rarfile)) throw new FileNotFoundException("压缩文件不存在");
            //建立解压缩目录
            if (!Directory.Exists(unRarPath))
                Directory.CreateDirectory(unRarPath);

            Process rarProcess = new Process();
            rarProcess.StartInfo.FileName = rarExecute;
            rarProcess.StartInfo.Arguments = "x " + rarfile + " " + unRarPath + " -o+ -y";
            rarProcess.StartInfo.CreateNoWindow = true;
            rarProcess.Start();
            while (!rarProcess.HasExited) { }
            int result = rarProcess.ExitCode;
            rarProcess.Close();
            if (result == 0)
                return true;
            else
                return false;
        }
    }
}
